
import csv                      # 输出数据
import codecs                   # 防止乱码
from datetime import datetime, timedelta

import pandas as pd             # 读取数据


# 时间设置特定格式
def process_date(date_str):
    date_number = ''.join(filter(str.isdigit, date_str))
    # 格式化字符串
    mode = '%Y%m%d%H%M'
    # 将日期字符串转换为datetime对象
    dt = datetime.strptime(date_number, mode)
    # 返回日期对象
    date_str = dt.strftime("%Y-%m-%d")
    return date_str


def main():
    my_tmp_list = []
    data_line = pd.read_csv(r"D:\file\Mechine_learning\before\2020_0.csv", encoding="gb18030", encoding_errors="ignore")
    # 2020年01月21日 22:59 time
    # res = data_line.iloc(0)[0][1]
    # print(process_date(data_line.iloc(0)[0][1]))
    # 2 sentiment
    # print(data_line.iloc(0)[0][10])
    # start = process_date('202001210000')
    # end = process_date('202001220000')
    # one_day = timedelta(days=1)
    for i in range(10):
        try:
            data_dict = {"date": process_date(data_line.iloc(0)[i][1])}
            my_tmp_list.append(data_dict)
        except ValueError as e:
            print(e)
    date_counts = {}
    for my_dict in my_tmp_list:
        date_str = my_dict['date']
        date_obj = datetime.strptime(date_str, "%Y-%m-%d").date()
        if date_obj in date_counts:
            date_counts[date_obj] += 1
        else:
            date_counts[date_obj] = 1

    # with codecs.open(r"D:\file\Python\data_series.csv", "a", "utf-8-sig") as file:
    #     csv_writer = csv.writer(file)
    #     csv_writer.writerow(
    #         ["关键词", "时间", "博主", "内容", "点赞", "转发", "评论", "confidence", "negative_prob", "positive_prob", "sentiment"]
    #     )
    #
    #     i = 0
    #     while i < 1000:
    #         pass
    df = pd.DataFrame(list(date_counts.items()), columns=['Date', 'Value'])
    df['Date'] = pd.to_datetime(df['Date'])  # 将Date列转换为日期格式
    df.to_excel(r"D:\file\Mechine_learning\before\2020_0.xlsx", index=False)


if __name__ == '__main__':
    main()








